package com.wei.ssm.dao;

import com.wei.ssm.domain.Permission;
import com.wei.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;
public interface IRoleDao {

    @Select("select * from role where id in (select roleId from users_role where userId=#{id})")    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "com.wei.ssm.dao.IpermissionDao.findPermissionById"))
    })
    List<Role> findRolesById(int id) throws Exception;

    @Select("select * from role where id in (select userId from users_role where roleId = #{id} )")
    List<Role> findRolesById2(int id) throws Exception;

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role);

    @Select("select * from role where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "com.wei.ssm.dao.IpermissionDao.findPermissionById"))
    })
    Role findById(int id);


    @Delete("delete from role where id = #{id}")
    void deleteById(int id);

    @Delete("delete from role_permission where permissionId = #{id}")
    void deletePermissonAndRoleById(int id);

    @Select("SELECT * FROM permission WHERE id NOT IN (SELECT permissionId FROM role_permission WHERE roleId = #{id})")
    List<Permission> findOtherPermission(@Param("id") int id);

    @Insert("insert into role_permission(permissionId,roleId) values(#{id},#{roleId})")
    void addPermissionToRole(@Param("roleId") int roleId, @Param("id") int id);
}
